package com.atguigu.utils;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

public class CyclicBarrierDemo {
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7,()->{
            System.out.println("收集七龙珠");
        });
        for (int i = 0; i < 10; i++) {
            int a = i;
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"\t"+"收集第"+a+"个龙珠");

                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                   e.printStackTrace();
                } catch (BrokenBarrierException e) {
                   e.printStackTrace();
                }
            },String.valueOf(i)).start();
        }
    }
}
